package com.allen.base.base.basic.extras

import android.graphics.Bitmap
import android.graphics.Color
import android.widget.ImageView
import androidx.annotation.ColorInt
import com.allen.base.utils.ViewUtils.dp2px
import com.bumptech.glide.Glide
import com.bumptech.glide.load.DataSource
import com.bumptech.glide.load.engine.DiskCacheStrategy
import com.bumptech.glide.load.engine.GlideException
import com.bumptech.glide.request.RequestListener
import com.bumptech.glide.request.RequestOptions
import com.bumptech.glide.request.target.Target
import jp.wasabeef.glide.transformations.BlurTransformation
import jp.wasabeef.glide.transformations.CropCircleWithBorderTransformation
import jp.wasabeef.glide.transformations.RoundedCornersTransformation


/**
 * Glide加载图片，可以指定圆角弧度。
 *
 * @param url 图片地址
 * @param round 圆角，单位dp
 * @param cornerType 圆角角度
 */
fun ImageView.load(
    url: String?,
    round: Float = 0f,
    cornerType: RoundedCornersTransformation.CornerType = RoundedCornersTransformation.CornerType.ALL,
    listener: OnImageLoadListener? = null
) {
    val path = url?.getUrl()
    if (round == 0f) {
        //判断是否是GIF图片
        if (isGif(path) && path?.endsWith(".jpg") == false) {
            Glide.with(this.context).asGif().diskCacheStrategy(DiskCacheStrategy.RESOURCE)
                .load(path).into(this)
        } else {
            Glide.with(this.context).load(path).into(this)
        }
    } else {
        val option = RequestOptions.bitmapTransform(
            RoundedCornersTransformation(dp2px(round).toInt(), 0, cornerType)
        ).dontAnimate()
        if (isGif(path) && path?.endsWith(".jpg") == false) {
            Glide.with(this.context).asGif().diskCacheStrategy(DiskCacheStrategy.RESOURCE)
                .load(path).apply(option).into(this)
        } else {
            Glide.with(this.context).asBitmap().load(path).apply(option)
                .listener(LoadRequestListener(listener)).into(this)
        }
    }
}

/**
 * 图片加载监听
 */
class LoadRequestListener(var listener: OnImageLoadListener?) :
    RequestListener<Bitmap> {
    override fun onLoadFailed(
        e: GlideException?,
        model: Any?,
        target: Target<Bitmap>?,
        isFirstResource: Boolean
    ): Boolean {
        return false
    }

    override fun onResourceReady(
        resource: Bitmap?,
        model: Any?,
        target: Target<Bitmap>?,
        dataSource: DataSource?,
        isFirstResource: Boolean
    ): Boolean {
        listener?.onSuccess(resource)
        return false
    }
}

fun ImageView.loadCircle(
    url: String?, borderSize: Int = 0, @ColorInt borderColor: Int = Color.WHITE
) {
    val option = RequestOptions.bitmapTransform(
        CropCircleWithBorderTransformation(borderSize, borderColor)
    ).dontAnimate()
    Glide.with(this.context).load(url).apply(option).into(this)
}

fun ImageView.loadBlur(url: String?, blurRadius: Int = 25, blurSampling: Int = 1) {
    Glide.with(this.context).load(url)
        .apply(RequestOptions.bitmapTransform(BlurTransformation(blurRadius, blurSampling)))
        .into(this)
}

/**
 * Glide加载图片，可以定义配置参数。
 *
 * @param url 图片地址
 * @param options 配置参数
 */
fun ImageView.load(url: String?, options: RequestOptions.() -> RequestOptions) {
    Glide.with(this.context).load(url).apply(RequestOptions().options()).into(this)
}

/**
 * 是否是GIF图
 */
fun isGif(any: Any?): Boolean {
    return if (any is String) {
        //不使用endWidth,避免有些带参数的url无法识别
        any.contains("gif", true)
    } else {
        false
    }
}


interface OnImageLoadListener {
    fun onSuccess(bitmap: Bitmap?)
}